package com.shushan.lockscreen;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.shushan.base.Constants;
import com.shushan.shushanbao.R;

/**
 * @ClassName: FreeWarnWindowManager
 * @Desciption: 小孩桌面自由到时提醒窗口管理类
 * @author: yichaohua
 * @date: 2018-04-11
 */
public class FreeWarnWindowManager implements View.OnClickListener {

    private static final String TAG = "FreeWarnWindowManager";

    private static FreeWarnWindowManager instance;
    private static Context context;
    private static boolean isWarnShow;
    private WindowManager mWindowManager;
    private View dialogView;

    public static FreeWarnWindowManager getInstance(Context context) {
        synchronized (FreeWarnWindowManager.class){
            if(instance == null){
                instance = new FreeWarnWindowManager();
            }

            FreeWarnWindowManager.context = context;
            return instance;
        }
    }

    public void showWarnDialog() {
        if(mWindowManager == null){
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){//8.0
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            params.type =  WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

        params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        // 不设置这个弹出框的透明遮罩显示为黑色
        params.format = PixelFormat.TRANSLUCENT;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.CENTER;

        if(dialogView == null){
            dialogView = LayoutInflater.from(context).inflate(R.layout.custom_dialog, null);

            dialogView.findViewById(R.id.dialog_cancel_btn).setVisibility(View.GONE);
            dialogView.findViewById(R.id.dialog_bottom_divide).setVisibility(View.GONE);
            Button okButton = dialogView.findViewById(R.id.dialog_ok_btn);
            okButton.setOnClickListener(this);
            okButton.setText("确定");

            TextView msgTextView = dialogView.findViewById(R.id.dialog_msg_tv);
            msgTextView.setText(Constants.WARN_TIP_FORWARD_TIME + "分钟后将锁屏，请及时存档。");

        }

        if(isWarnShow){
            try {
                mWindowManager.updateViewLayout(dialogView, params);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
                mWindowManager.addView(dialogView, params);
            }

        }else {
            try {
                mWindowManager.addView(dialogView, params);
            }catch (IllegalStateException e){
                e.printStackTrace();
                mWindowManager.removeView(dialogView);
                mWindowManager.addView(dialogView, params);
            }

        }

        isWarnShow = true;
    }


    public void dismissWarnDialog() {
        if(isWarnShow && mWindowManager != null && dialogView != null){

            try {
                mWindowManager.removeView(dialogView);
            }catch (IllegalArgumentException e){
                e.printStackTrace();
            }

            isWarnShow = false;
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_ok_btn:
                if(mWindowManager != null && dialogView != null){
                    mWindowManager.removeView(dialogView);
                    isWarnShow = false;
                }
                break;
            default:
                break;
        }
    }
}
